/**
 * 支付相关服务
 */

const util = require('../utils/util.js');
const api = require('../config/api.js');

/**
 * 判断用户是否登录
 */
function payOrder(orderId) {
  return new Promise(function (resolve, reject) {
    wx.showLoading({
      mask: true,
      title: '支付中...',
    })
    util.request(api.PayRent, {
      billId: orderId
    }).then((res) => {
      console.log(res)
      if (res.errno === 0) {
        if (util.isNullOrEmpty(res.data.js_prepay_info)){
          util.confirm.modal(res.data.message);
          resolve('fail');
        }else{
          const payParam = JSON.parse(res.data.js_prepay_info);
          wx.requestPayment({
            'timeStamp': payParam.timeStamp,
            'nonceStr': payParam.nonceStr,
            'package': payParam.package,
            'signType': payParam.signType,
            'paySign': payParam.paySign,
            'success': function (res) {
              resolve(res);
            },
            'fail': function (res) {
              reject(res);
            },
            'complete': function (res) {
              reject(res);
            }
          });
        }
      } else {
        reject(res);
      }
    }).catch((err) => {
      reject(err);
    });
  });
}


module.exports = {
  payOrder,
};











